package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.SelfTeamGroupEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamOfferEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class SelfTeamGroupAdapter extends BaseRecycleAdapter<SelfTeamGroupEntity> {

    private RequestManager requestManager;

    public SelfTeamGroupAdapter(Context context, List<SelfTeamGroupEntity> data, RequestManager requestManager) {
        super(context, R.layout.item_self_team_group, data);
        this.requestManager = requestManager;
    }

    @Override
    public void convert(ItemViewHolder holder, SelfTeamGroupEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ImageView workerAvatar = holder.itemView.findViewById(R.id.iv_worker_avatar);
        RequestOptions options = new RequestOptions();
        options.placeholder(R.mipmap.worker_avatar);
        options.transforms(new CenterCrop(), new RoundedCorners(8));
        if (TextUtils.isEmpty(entity.getAvatarAddress())) {
            requestManager.load(R.mipmap.worker_avatar).into(workerAvatar);
        } else {
            requestManager.load(entity.getAvatarAddress()).apply(options).into(workerAvatar);
        }
        TextView TvWorkerInfo = holder.itemView.findViewById(R.id.workerInfo);
        TvWorkerInfo.setText(String.format("%s岁  |  %s年工龄", entity.getAge(), entity.getWorkingAge()));
        if (null == entity.getIdentity() || TextUtils.isEmpty(entity.getIdentity())) {
            holder.itemView.findViewById(R.id.tv_identity).setVisibility(View.GONE);
        } else {
            holder.itemView.findViewById(R.id.tv_identity).setVisibility(View.VISIBLE);
        }
        if (null == entity.getWorkTypeName() || TextUtils.isEmpty(entity.getWorkTypeName())) {
            holder.itemView.findViewById(R.id.tv_worker_type).setVisibility(View.GONE);
        } else {
            holder.itemView.findViewById(R.id.tv_worker_type).setVisibility(View.VISIBLE);
        }
        if (null == entity.getProficiencyName() || TextUtils.isEmpty(entity.getProficiencyName())) {
            holder.itemView.findViewById(R.id.tv_proficiencyName).setVisibility(View.GONE);
        } else {
            holder.itemView.findViewById(R.id.tv_proficiencyName).setVisibility(View.VISIBLE);
        }
        holder.itemView.findViewById(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.delete(entity);
            }
        });
        holder.itemView.findViewById(R.id.btn_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.edit(entity);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(entity);
            }
        });
    }

    private GroupRecyclerItemClick listener;

    public interface GroupRecyclerItemClick {

        void delete(SelfTeamGroupEntity entity);

        void edit(SelfTeamGroupEntity entity);

        void onItemClick(SelfTeamGroupEntity entity);
    }

    public void setListener(GroupRecyclerItemClick listener) {
        this.listener = listener;
    }
}
